package framework.web.rpc.adapter;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

public abstract class JsonAdapter implements IAdapter {
	protected JSONObject	jso	= new JSONObject();

	@Override
	public abstract String translate(Object o);

	//--------------------------------------------------------------------------------------------
	public void clear() {
		jso.clear();
	}

	public void put(String key, Object value) {
		try {
			jso.put(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void putArray(String key, List<Map<String, Object>> value) {
		try {
			JSONArray array = new JSONArray();
			for (int i = 0; i < value.size(); i++) {
				array.put(value.get(i));
			}
			jso.put(key, array);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String toJsonString(Map<String, Object> value) {
		try {
			JSONArray array = new JSONArray();
			array.put(value);
			return array.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String toJsonString() {
		return jso.toString();
	}

}
